DELETE articles

PUT articles
{
  "mappings": {
    "properties": {
      "title_completion":{
        "type": "completion"
      }
    }
  }
}

POST articles/_bulk
{ "index" : { } }
{ "title_completion": "lucene is very cool"}
{ "index" : { } }
{ "title_completion": "Elasticsearch builds on top of lucene"}
{ "index" : { } }
{ "title_completion": "Elasticsearch rocks"}
{ "index" : { } }
{ "title_completion": "elastic is the company behind ELK stack"}
{ "index" : { } }
{ "title_completion": "Elk stack rocks"}
{ "index" : {} }

// 单独一个e前缀
POST articles/_search?pretty
{
  "size": 0,
  "suggest": {
    "article-suggester": {
      "prefix": "e ",
      "completion": {
        "field": "title_completion"
      }
    }
  }
}

// elk前缀
POST articles/_search?pretty
{
  "size": 0,
  "suggest": {
    "article-suggester": {
      "prefix": "elk ",
      "completion": {
        "field": "title_completion"
      }
    }
  }
}

// 另一个例子
get _analyze
{
  "analyzer": "ik_max_word",
  "text": "地下城"
}

DELETE goods

PUT goods
{
    "mappings": {
        "properties": {
            "completion": {
                "type": "completion",
                "analyzer": "ik_smart"
            },
            "name": {
                "type": "text",
                "analyzer": "ik_max_word"
            }
        }
    }
}

get goods

POST goods/_bulk
{    "index": {}}
{ "completion": ["地砖"], "name": "奥克idu地砖"}
{ "index" : { } }
{ "completion": ["地板"], "name": "xx地板"}
{ "index" : { } }
{ "completion": ["地板", "德牧"], "name": "德牧地板"}
{ "index" : { } }
{ "completion": ["地板", "大自然"], "name": "大自然地板"}
{ "index" : { } }
{ "completion": ["地下城"], "name": "地下城与勇士"}


POST goods/_search?pretty
{
  "size": 0,
  "suggest": {
    "goods-suggester": {
      "prefix": "地",
      "completion": {
        "skip_duplicates": true,
        "field": "completion"
      }
    }
  }
}

get goods/_search

// Context Suggester
// 是Completion Suggester的扩展, 比如输入star, 如果在游戏上下文, 就推荐star wars, 在餐饮上下文, 就推荐 starbucks
// 支持两种类型的Context: Category和Geo

DELETE comments

// 定义mapping
PUT comments
{
    "mappings": {
        "properties": {
            "comment_autocomplete": {
                "type": "completion",
                "contexts": [
                    {
                        "type": "category",
                        "name": "comment_category"
                    }
                ]
            }
        }
    }
}
// 插入数据
POST comments/_doc
{
  "comment":"I love the star war movies",
  "comment_autocomplete":{
    "input":["star wars"],
    "contexts":{
      "comment_category":"movies"
    }
  }
}
// 插入数据
POST comments/_doc
{
  "comment":"Where can I find a Starbucks",
  "comment_autocomplete":{
    "input":["starbucks"],
    "contexts":{
      "comment_category":"coffee"
    }
  }
}

// 搜索
POST comments/_search
{
  "suggest": {
    "context_suggester": {
      "prefix": "sta",
      "completion":{
        "field":"comment_autocomplete",
        "contexts":{
          "comment_category":"coffee"
        }
      }
    }
  }
}